﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace TimeQuest
{
    class InputHandler
    {
        public static KeyboardState LastKeyboardState;
        public static int MouseTextureID;
        public static MouseState LastMouseState;
        public static Cursors CurrentCursor = Cursors.Look;

        #region Mouse Methods

        public static void DrawMouse(SpriteBatch sb, MouseState ms, float Opacity, Rectangle Size)
        {
            sb.Draw(ContentHandler.GetTexture(MouseTextureID),
                new Rectangle(ms.X, ms.Y, Size.Width, Size.Height),
                new Rectangle(Size.Width * (int)CurrentCursor, 0, Size.Width, Size.Height),
                new Color(Color.White, Opacity));

        }
        public static void AdvanceCursor()
        {
            int i = (int)CurrentCursor;
            i++;
            CurrentCursor = (Cursors)i;
            if (!Enum.IsDefined(typeof(Cursors), CurrentCursor))
                CurrentCursor = (Cursors)0;

        }
        public static void SetCursor(Cursors cursor)
        {
            CurrentCursor = cursor;
        }

        internal static bool LeftClicked(MouseState ms)
        {
            if (ms.LeftButton == ButtonState.Pressed &&
                LastMouseState.LeftButton == ButtonState.Released)
                return true;
            return false;
        }

        internal static bool RightClicked(MouseState ms)
        {
            if (ms.RightButton == ButtonState.Pressed &&
                LastMouseState.RightButton == ButtonState.Released)
                return true;
            return false;
        }

        #endregion

        #region Keyboard Methods

        public static bool JustPressed(KeyboardState ks, Keys k)
        {
            if (LastKeyboardState.IsKeyUp(k) &&
                ks.IsKeyDown(k))
                return true;
            return false;
        }
        public static IList<Keys> JustPressedList(KeyboardState ks)
        {
            IList<Keys> retVal = new List<Keys>();
            foreach (Keys k in ks.GetPressedKeys())
            {
                if (JustPressed(ks, k))
                    retVal.Add(k);
            }
            return retVal;
        }

        #endregion
    }
}
